//
//  SNFriendDetailVc.m
//  project
//
//  Created by 黄建华 on 2018/8/10.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNFriendDetailVc.h"
#import "SNInfoView.h"
#import "SBStepScrollView.h"

#define FRIENDCELLHEIGHT FitSimpleSize(300, 300, 300, 300, 300)

@interface SNFriendDetailVc ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property (nonatomic, strong) UIView      *topView;
@property (nonatomic, strong) SNEncourageButton    *button1;
@property (nonatomic, strong) SNEncourageButton    *button2;
@property (nonatomic, strong) UIImageView *headImageV;
@property (nonatomic, strong) UIView      *headView;
@property (nonatomic, strong) UILabel     *userIdLabel;
@property (nonatomic, strong) UIImageView *genderImageV;

@property(nonatomic, strong) UIView          *backView; // 个人4个板块
@property(nonatomic, strong) SNInfoView      *firstView;
@property(nonatomic, strong) SNInfoView      *secondView;
@property(nonatomic, strong) SNInfoView      *thirdView;
@property(nonatomic, strong) SNInfoView      *forthView;

@end

@implementation SNFriendDetailVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    NSLog(@"model nick name >>>%@ ID:%d",_model.nickName,_model.userId);
    
    
    _friendModel           = [FriendModel getFriendModelWith:StrByInt(_model.userId)];
    _friendModel.imagePath = _model.imagePathUrlString;
    _friendModel.nickName  = _model.nickName;
    _friendModel.remark    = _model.remark;
    _friendModel.sign      = _model.sign;
    _friendModel.gender    = _model.gender;
    [_friendModel updateToDB];
    NSLog(@"My Friend >>>%@ ID:%@", _friendModel.nickName, _friendModel.user_id);
    
    
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage     *backImage     = UIImageNamed(@"背景");
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, 250)];
    backImageView.userInteractionEnabled = YES;
    backImageView.image                  = backImage;
    [self.view addSubview:backImageView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    lable.textColor     = [UIColor blackColor];
    lable.font          = SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    //    lable.text = HHH_Text(@"好友健康");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入白"];
    [leftButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
    //    [self.view addSubview:shadowLine];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(screenWidth - 44 -5, 33 + FitSimpleIpx(0, 20), 44, 44);
    [shareButton setImageNormal:@"分享"];
    shareButton.center = CGPointMake(screenWidth - 22 -10, leftButton.centerY);
    [self.view addSubview:shareButton];
    [shareButton addTarget:self action:@selector(shareButtonFriendButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    _topView                 = [[UIView alloc] init];
    _topView.frame           = SNFRAME(0, shadowLine.bottom, screenWidth, 180);
    _topView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_topView];

    _headImageV                     = [[UIImageView alloc] init];
    _headImageV.frame               = SNFRAME(0, 0, 120, 120);
    _headImageV.layer.cornerRadius  = _headImageV.width/2.0;
    _headImageV.layer.masksToBounds = YES;
    [_headImageV sd_setImageWithURL:[NSURL URLWithString:_model.imagePathUrlString] placeholderImage:[UIImage imageNamed:@"loginhead"]];
    _headImageV.center              = CGPointMake(screenWidth/2, _topView.height/2 -10);
    [_topView addSubview:_headImageV];
    
    _button1              = [[SNEncourageButton alloc] init];
    _button1.frame        = SNFRAME(0, 0, 50, 50);
    _button1.center       = CGPointMake(_headImageV.left - 50, _headImageV.centerY);
    _button1.tag          = 1;
    _button1.imageNormal  = @"点赞1";
    _button1.imageSelecte = @"点赞";
    [_button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:_button1];
    
    UILabel *labale1 = [[UILabel alloc] init];
    labale1.textColor     = SNCOLORWHITE;
    labale1.font          = SNFONT(13.0);
    labale1.text          = SN_Text(@"点赞");
    labale1.frame         = SNFRAME(_button1.left -25, _button1.bottom + 7, 100, 20);
    labale1.textAlignment = NSTextAlignmentCenter;
    [_topView addSubview:labale1];
    
    _button2              = [[SNEncourageButton alloc] init];
    _button2.frame        = SNFRAME(0, 0, 50, 50);
    _button2.center       = CGPointMake(_headImageV.right + 50, _headImageV.centerY);
    _button2.tag          = 2;
    _button2.imageNormal  = @"鼓励1";
    _button2.imageSelecte = @"鼓励";
    [_button2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:_button2];
    
    UILabel *labale2 = [[UILabel alloc] init];
    labale2.textColor     = SNCOLORWHITE;
    labale2.font          = SNFONT(13.0);
    labale2.text          = SN_Text(@"鼓励");
    labale2.textAlignment = NSTextAlignmentCenter;
    labale2.frame         = SNFRAME(_button2.left -25, _button2.bottom + 7, 100, 20);
    [_topView addSubview:labale2];
    
    
    UILabel *nickLabel = [[UILabel alloc] init];
    nickLabel.textColor     = SNCOLORWHITE;
    nickLabel.font          = SNFONT(17.0);
    nickLabel.frame         = SNFRAME(_headImageV.centerX - 80, _headImageV.bottom + 10, 160, 20);
    nickLabel.textAlignment = NSTextAlignmentCenter;
    nickLabel.text          = _friendModel.showNickName;
    [_topView addSubview:nickLabel];
    
    _userIdLabel               = [[UILabel alloc] init];
    _userIdLabel.textColor     = SNCOLORWHITE;
    _userIdLabel.font          = SNFONT(15.0);
    _userIdLabel.frame         = SNFRAME(_headImageV.centerX - 80, nickLabel.bottom + 3, 160, 20);
    _userIdLabel.textAlignment = NSTextAlignmentCenter;
    _userIdLabel.text          = [NSString stringWithFormat:@"ID:%@", _friendModel.user_id];
    [_topView addSubview:_userIdLabel];
    
    _genderImageV = [[UIImageView alloc] init];
    [_topView addSubview:_genderImageV];
    CGFloat width = [self getWidthWithText:nickLabel.text height:20 font:17.0] + 10;
    nickLabel.frame         = SNFRAME(_headImageV.centerX - width/2, _headImageV.bottom + 10, width, 20);
    _genderImageV.frame = SNFRAME(nickLabel.right +5 , nickLabel.top - 7, 33, 33);
    if (_model.gender == 1) {
        _genderImageV.image = [UIImage image:@"F男"];
    } else {
        _genderImageV.image = [UIImage image:@"F女"];
    }
    
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.delegate = self;
    _scrollView.frame = CGRectMake(0, backImageView.bottom, HHHWIDTH, HHHHEIGHT-backImageView.bottom);
    [self.view addSubview:_scrollView];

    _backView       = [[UIView alloc] init];
    _backView.frame = SNFRAME(0, 0, screenWidth, FitH(100) +FitH(100) +10);
    
    _firstView = [[SNInfoView alloc]initWithFrame:CGRectMake(FitW(30), FitH(5), HHHWIDTH/2-FitW(25), FitH(100)) withType:0];
    [_backView addSubview:_firstView];
    
    _secondView = [[SNInfoView alloc]initWithFrame:CGRectMake(HHHWIDTH/2+FitW(30), FitH(5), HHHWIDTH/2-FitW(25), FitH(100)) withType:1];
    [_backView addSubview:_secondView];
    
    _thirdView = [[SNInfoView alloc]initWithFrame:CGRectMake(FitW(30), _firstView.bottom +10, _firstView.width, FitH(100)) withType:2];
    [_backView addSubview:_thirdView];
    
    _forthView = [[SNInfoView alloc]initWithFrame:CGRectMake(HHHWIDTH/2+FitW(30), _thirdView.top, _secondView.width, FitH(100)) withType:3];
    [_backView addSubview:_forthView];
    
    _scrollView.contentSize = CGSizeMake(0, _backView.height + FRIENDCELLHEIGHT *4);
    
    // 1
    [_scrollView addSubview:_backView];
    // 2
    _stepView = [[SBStepScrollView alloc] initWithFrame:SNFRAME(0, _backView.height, screenWidth, FRIENDCELLHEIGHT)];
    [_scrollView addSubview:_stepView];
    // 3
    _sleepView = [[SBSleepScrollView alloc]  initWithFrame:SNFRAME(0, _backView.height + FRIENDCELLHEIGHT, screenWidth, FRIENDCELLHEIGHT)];
    [_scrollView addSubview:_sleepView];
    // 4
    _heartView = [[SNheartView alloc] initWithFrame:SNFRAME(0, _backView.height + FRIENDCELLHEIGHT*2, screenWidth, FRIENDCELLHEIGHT)];
    [_scrollView addSubview:_heartView];
    // 5
    _bpAndOxView = [[SNBpAndOxView alloc] initWithFrame:SNFRAME(0, _backView.height + FRIENDCELLHEIGHT*3, screenWidth, FRIENDCELLHEIGHT)];
    [_scrollView addSubview:_bpAndOxView];
    // other
    UIImageView *shadowLine0 = [[UIImageView alloc] init];
    shadowLine0.frame = SNFRAME(0,_stepView.top, HHHWIDTH, 2);
    shadowLine0.image = [UIImage image:@"lineImage"];
    [_scrollView addSubview:shadowLine0];
    
    UIImageView *shadowLine1 = [[UIImageView alloc] init];
    shadowLine1.frame = SNFRAME(0,_stepView.bottom, HHHWIDTH, 2);
    shadowLine1.image = [UIImage image:@"lineImage"];
    [_scrollView addSubview:shadowLine1];
    
    UIImageView *shadowLine2 = [[UIImageView alloc] init];
    shadowLine2.frame = SNFRAME(0,_sleepView.bottom, HHHWIDTH, 2);
    shadowLine2.image = [UIImage image:@"lineImage"];
    [_scrollView addSubview:shadowLine2];
    
    UIImageView *shadowLine3 = [[UIImageView alloc] init];
    shadowLine3.frame = SNFRAME(0,_heartView.bottom, HHHWIDTH, 2);
    shadowLine3.image = [UIImage image:@"lineImage"];
    [_scrollView addSubview:shadowLine3];
    
    UIImageView *shadowLine4 = [[UIImageView alloc] init];
    shadowLine4.frame = SNFRAME(0,_bpAndOxView.bottom, HHHWIDTH, 2);
    shadowLine4.image = [UIImage image:@"lineImage"];
    [_scrollView addSubview:shadowLine4];
    
    
    [SN_Net friendWithSearchHomePage:_friendModel.user_id success:^(id object) {
        NSDictionary *dic = object;
        [_friendModel updateToSelWith:dic];
        [self updateCurrentUi];
    } fail:^(id object) {
    }];
}

- (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(CGFloat)font {
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                     context:nil];
    
    return rect.size.width;
}

// 获取用户统计的数据并刷新
- (void)updateCurrentUi {
    if (_friendModel.isBest == 1) {
        _button1.selected = YES;
    } else {
        _button1.selected = NO;
    }
    
    if (_friendModel.isSupport == 1) {
        _button2.selected = YES;
    } else {
        _button2.selected = NO;
    }
    //1  四个小模块
    NSLog(@"value1 >>>%@", _friendModel.showValue1);
    NSLog(@"value2 >>>%@", _friendModel.showValue2);
    NSLog(@"value3 >>>%@", _friendModel.showValue3);
    NSLog(@"value4 >>>%@", _friendModel.showValue4);
    
    NSString *value1 = _friendModel.showValue1;
    NSString *value2 = _friendModel.showValue2;
    NSString *value3 = _friendModel.showValue3;
    NSString *value4 = _friendModel.showValue4;
    
    NSString *value11 = _friendModel.showValue11;
    NSString *value22 = _friendModel.showValue22;
    NSString *value33 = _friendModel.showValue33;
    NSString *value44 = _friendModel.showValue44;
    
    
    _firstView.contentStr = [NSString stringWithFormat:@"%@ %@",value1, SN_Text(@"步")];
    _firstView.dateStr    = value11;
    
    _secondView.contentStr = [NSString stringWithFormat:@"%@ %@",value2, SN_Text(@"天")];
    _secondView.dateStr = value22;
    _thirdView.contentStr = [NSString stringWithFormat:@"%@ %@",value3, SN_Text(@"步")];
    _thirdView.dateStr = value33;
    _forthView.contentStr = [NSString stringWithFormat:@"%@ %@",value4, SN_Text(@"步")];
    _forthView.dateStr = value44;
    
    // 更新step
    [_stepView loadDataWithModel:_friendModel];
    [_sleepView loadDataWithModel:_friendModel];
    [_heartView loadDataWithModel:_friendModel];
    [_bpAndOxView loadDataWithModel:_friendModel];
}

- (void)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        [_button1 EncourageButtonClickWithImageName:@"点赞"];
    }
    else
    {
        [_button2 EncourageButtonClickWithImageName:@"鼓励"];
    }
    
    [SN_Net friendClickWithType:sender.tag userid:_friendModel.user_id Success:^(id object) {
        if (sender.tag == 1) {
            SHOWMBProgressHUD(SN_Text(@"点赞成功"), nil, nil, NO, 1.0);
            _button1.selected = YES;
        } else if (sender.tag == 2) {
            SHOWMBProgressHUD(SN_Text(@"鼓励成功"), nil, nil, NO, 1.0);
            _button2.selected = YES;
        }
    } fail:^(id object) {
    }];
}

- (void)shareButtonFriendButtonClick {
    UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
    
    [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:image];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                        UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                        UIActivityTypeOpenInIBooks]];
    activityViewController.excludedActivityTypes = excludedActivityTypes;

    [self presentViewController:activityViewController animated:YES completion:nil];
    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
    };
}

- (void)backButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
